//
// Created by wuzhuya on 23-5-8.
//

#include "computer.h"


/*
int main() {
    while (1) {
        int a;
        printf("请输入您的操作:\n1. 进行大数相加\n2. 进行大数相乘\n3. 退出程序\n");
        scanf("%d", &a);
        char representation1[1024];
        char representation2[1024];
        NODE *head1, *head2, *result;
        if (a == 1) {
            input(representation1, representation2);
            initLink(&head1);
            initLink(&head2);
            repToLink(head1, representation1);
            repToLink(head2, representation2);
            NODE *head[] = {head1, head2};
            // 进行大数相加的相关操作
//            add(head1, head2, &result);
            add(head, 0, 2);
            printf("%s+%s=", representation1, representation2);
            show(head[1]);
        } else if (a == 2) {
            input(representation1, representation2);
            initLink(&head1);
            initLink(&head2);
            repToLink(head1, representation1);
            repToLink(head2, representation2);

            // 进行大数相加的相关操作
            mul(head1, head2, &result);
            show(result);
        } else if (a == 3) {
            break;
        } else {
            printf("请输入合法的字符\n");
        }
    }
    return 0;
}*/

void input(char *rep1, char *rep2) {
    memset(rep1, 0, 1024);
    memset(rep2, 0, 1024);
    printf("请输入第一个数字: ");
    scanf("%s", rep1);
    printf("请输入第二个数字: ");
    scanf("%s", rep2);
}

/*void add(NODE *head1, NODE *head2, NODE **result) {
    NODE *p1 = lastNode(head1);
    NODE *p2 = lastNode(head2);
    initLink(result);
    // 表示进位
    int n = 0;
    while (p1 != head1 && p2 != head2) {
        int a = p1->data->c - 48;
        int b = p2->data->c - 48;
        int c = a + b + n;
        if (c >= 10) {
            n = 1;
            c = c - 10;
        } else {
            n = 0;
        }
        addPriorNode(*result, c + 48);
        p1 = p1->prior;
        p2 = p2->prior;
    }

    while (p1 != head1) {
        int a = p1->data->c - 48 + n;
        if (a >= 10) {
            n = 1;
            a = a - 10;
        } else {
            n = 0;
        }
        addPriorNode(*result, a + 48);
        p1 = p1->prior;
    }
    while (p2 != head2) {
        int a = p2->data->c - 48 + n;
        if (a >= 10) {
            n = 1;
            a = a - 10;
        } else {
            n = 0;
        }
        addPriorNode(*result, a + 48);
        p2 = p2->prior;
    }

}*/

void addBig(NODE *head[], int p, int len) {
    NODE *node;
    initLink(&node);
    if (p < len - 1) {
        NODE *head1 = head[p], *head2 = head[p + 1];
        NODE *p1 = lastNode(head1);
        NODE *p2 = lastNode(head2);
        // 表示进位
        int n = 0;
        while (p1 != head1 && p2 != head2) {
            int a = p1->data->c - 48;
            int b = p2->data->c - 48;
            int c = a + b + n;
            if (c >= 10) {
                n = 1;
                c = c - 10;
            } else {
                n = 0;
            }
            addPriorNode(node, c + 48);
            p1 = p1->prior;
            p2 = p2->prior;
        }
        while (p1 != head1) {
            int a = p1->data->c - 48 + n;
            if (a >= 10) {
                n = 1;
                a = a - 10;
            } else {
                n = 0;
            }
            addPriorNode(node, a + 48);
            p1 = p1->prior;
        }
        while (p2 != head2) {
            int a = p2->data->c - 48 + n;
            if (a >= 10) {
                n = 1;
                a = a - 10;
            } else {
                n = 0;
            }
            addPriorNode(node, a + 48);
            p2 = p2->prior;
        }
        head[p + 1] = node;
        addBig(head, p + 1, len);
    }
}

void mulBig(NODE *head1, NODE *head2, NODE **result) {
    initLink(result);
    int head1Len = length(head1);
    NODE *res[head1Len];
    NODE *p1 = lastNode(head1);
    NODE *p2 = lastNode(head2);
    for (int i = 0; i < head1Len; ++i) {
        initLink(&res[i]);
        for (int j = 0; j < i; ++j) {
            addPriorNode(res[i], '0');
        }
        int n = 0;
        int a = p1->data->c - 48;
        while (p2 != head2) {
            int b = p2->data->c - 48;
            int c = a * b + n;
            if (c >= 10) {
                n = c / 10;
                c = c % 10;
            } else {
                n = 0;
            }
            addPriorNode(res[i], c + 48);
            p2 = p2->prior;
        }
        if (n != 0) {
            addPriorNode(res[i], n + 48);
        }
        p1 = p1->prior;
        p2 = lastNode(head2);
    }
    addBig(res, 0, head1Len);
    *result = res[head1Len - 1];
}